
package net.zjitc.controller;

import net.zjitc.utils.APIResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class FileUploadController {

    @Value("${file.upload-dir}")
    private String uploadDir;

    @Value("${file1.upload-dir}")
    private String uploadDir1;

    @Value("static/image/category")
    private String uploadDirCategory;

    @PostMapping("/category")
    @ResponseBody
    public APIResponse<String> uploadFileCategory(@RequestParam("file") MultipartFile file) {
        APIResponse<String> apiResponse = new APIResponse<>();
        if (file.isEmpty()) {
            apiResponse.fail(1000, "图片不能为空", null);
            return apiResponse;
        }

        String fileName = file.getOriginalFilename();

        try {
            // 创建上传目录（如果不存在）
            Path uploadPath = Paths.get(uploadDirCategory);
            if (!Files.exists(uploadPath)) {
                Files.createDirectories(uploadPath);
            }

            // 保存文件
            String newFileName = UUID.randomUUID().toString() +'-'+ fileName;
            Path path = Paths.get(uploadDirCategory + newFileName);
            Files.copy(file.getInputStream(), path);

            apiResponse.success(null, newFileName);
            return apiResponse;
        } catch (IOException e) {
            e.printStackTrace();
            apiResponse.fail(1000, "文件上传失败", null);
            return apiResponse;
        }

    }

    @PostMapping("/picture")
    @ResponseBody
    public APIResponse<String> uploadFile(@RequestParam("file") MultipartFile file) {
        APIResponse<String> apiResponse = new APIResponse<>();
        if (file.isEmpty()) {
            apiResponse.fail(1000, "图片不能为空", null);
            return apiResponse;
        }

        String fileName = file.getOriginalFilename();

        try {
            // 创建上传目录（如果不存在）
            Path uploadPath = Paths.get(uploadDir1);
            if (!Files.exists(uploadPath)) {
                Files.createDirectories(uploadPath);
            }

            // 保存文件
            String newFileName = UUID.randomUUID().toString() +'-'+ fileName;
            Path path = Paths.get(uploadDir1 + newFileName);
            Files.copy(file.getInputStream(), path);

            apiResponse.success(null, newFileName);
            return apiResponse;
        } catch (IOException e) {
            e.printStackTrace();
            apiResponse.fail(1000, "文件上传失败", null);
            return apiResponse;
        }
    }
    @PostMapping("/goodspicture")
    @ResponseBody
    public APIResponse<String> uploadFile1(@RequestParam("file") MultipartFile file) {
        APIResponse<String> apiResponse = new APIResponse<>();
        if (file.isEmpty()) {
            apiResponse.fail(1000, "图片不能为空", null);
            return apiResponse;
        }

        String fileName = file.getOriginalFilename();

        try {
            // 创建上传目录（如果不存在）
            Path uploadPath = Paths.get(uploadDir);
            if (!Files.exists(uploadPath)) {
                Files.createDirectories(uploadPath);
            }

            // 保存文件
            String newFileName = UUID.randomUUID().toString() +'-'+ fileName;
            Path path = Paths.get(uploadDir + newFileName);
            Files.copy(file.getInputStream(), path);

            apiResponse.success(null, newFileName);
            return apiResponse;
        } catch (IOException e) {
            e.printStackTrace();
            apiResponse.fail(1000, "文件上传失败", null);
            return apiResponse;
        }
    }
}
